package service;

import beans.ClassBean;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import common.model.Lukedate;
import common.model.Record;
import util.IDUtil;

import java.text.SimpleDateFormat;
import java.util.*;

public class OrderService {

    List<Record> records;
    Date startTime;
    Date endTime;

    public OrderService(){

    }

    public OrderService(int page){
        Calendar calendar=Calendar.getInstance();
        if(page>=2){
            calendar.add(Calendar.DATE,7*(page-1));
        }
        int day_of_week=calendar.get(Calendar.DAY_OF_WEEK);
        if(day_of_week==1){
            day_of_week=1;
        }
        day_of_week=day_of_week-1;
        calendar.add(Calendar.DATE,-(day_of_week-1));
        calendar.set(Calendar.HOUR_OF_DAY,0);
        this.startTime=calendar.getTime();
        calendar.add(Calendar.DATE,5);
        calendar.set(Calendar.HOUR_OF_DAY,0);
        this.endTime=calendar.getTime();
        System.out.println(startTime+"   "+endTime);
        this.records=new Record().find("select * from record where day >= ? and day<= ?",startTime,endTime);
    }

    public String onState(String xingqi,String jieci){
        Iterator<Record> iterator=records.iterator();
        while(iterator.hasNext()){
            Record record=iterator.next();
            if(record.getXingqi().equals(xingqi)&&record.getJieci().equals(jieci)){
                return record.getId();
            }
        }
        return null;
    }

    public List<ClassBean> getOneWeekDatas(int page){
        List<Lukedate> lukedates=new Lukedate().find("select * from lukedate order by jieci");
        List<ClassBean> classBeans=new ArrayList<>();
        Iterator<Lukedate> iterator=lukedates.iterator();
        while(iterator.hasNext()){
            Lukedate lukedate=iterator.next();
            Calendar calendar=Calendar.getInstance();
            calendar.setTime(startTime);
            for(int i=1;i<=5;i++){
                ClassBean classBean=new ClassBean();
                classBean.setDay(calendar.getTime());
                calendar.set(Calendar.HOUR_OF_DAY,8);
                calendar.add(Calendar.DATE,1);
                classBean.setJieci(lukedate.getJieci()+"");
                classBean.setXingqi(getXingQi(i));
                Calendar calendar1 = Calendar.getInstance();
                Date date=new Date();
                calendar1.setTime(date);
                if(lukedate.get("xingqi"+i)==null||lukedate.get("xingqi"+i).toString().length()==0){
                    classBean.setSheying("");
                    classBean.setState(false);
                }else if(page==1&&i<=calendar1.get(Calendar.DAY_OF_WEEK)-1){
                    classBean.setSheying(lukedate.get("xingqi"+i));
                    classBean.setState(false);
                }else{
                    classBean.setSheying(lukedate.get("xingqi"+i));
                    String onState=onState(getXingQi(i),lukedate.getJieci()+"");
                    classBean.setId(onState);
                    classBean.setState(true);
                }
                classBeans.add(classBean);
            }
        }
        return classBeans;
    }

    public String getXingQi(int xingqi){
        switch (xingqi){
            case 1:return "一";
            case 2:return "二";
            case 3:return "三";
            case 4:return "四";
            case 5:return "五";
        }
        return "";
    }

    public int yuyue(String sheying,String name,int jieci,String day,String xingqi){
        try{
            Date dayDate=null;
            SimpleDateFormat simpleDateFormat=new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.UK);
            dayDate=simpleDateFormat.parse(day);
            Record record=new Record();
            Calendar  calendar=Calendar.getInstance();
            calendar.setTime(dayDate);
            calendar.set(Calendar.HOUR_OF_DAY,8);
            record.setDay(calendar.getTime());
            record.setId(IDUtil.getRandomID());
            record.setJieci(jieci+"");
            record.setName(name);
            record.setSheying(sheying);
            record.setXingqi(xingqi);
            record.setWhen(new Date());
            record.setState(0L);
            if(record.save()){
                return 0;
            }
            return -1;
        }catch (Exception e){
            e.printStackTrace();
            return -1;
        }
    }

    public int quxiaoyuyue(String id,String name){
        Record record = new Record().findById(id);
        if(record.getName().equals(name)){
            record.delete();
            return 0;
        }
        return -1;
    }

    public static void main(String[] args) {
//        new OrderService().getOneWeekDatas(2);
        System.out.println("run");
    }

    public List<ClassBean> getMyDatas(String who){
        List<ClassBean> classBeans=new ArrayList<>();
        List<Record> records=new Record().find("select * from record where name=? order by day desc",who);
        Iterator<Record> iterator=records.iterator();
        while(iterator.hasNext()){
            Record record=iterator.next();
            ClassBean bean=new ClassBean();
            bean.setSheying(record.getSheying());
            bean.setDay(record.getDay());
            bean.setId(record.getId());
            bean.setJieci(record.getJieci());
            bean.setXingqi(record.getXingqi());
            System.out.println(record.getState());
            if(record.getState()==null||record.getState()==0){
                bean.setState(false);
            }else{
                bean.setState(true);
            }
            classBeans.add(bean);
        }
        return classBeans;
    }
}
